package com.pangda.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.pangda.common.domain.PageResult;
import com.pangda.common.domain.ResponseResult;
import com.pangda.common.utils.FileUtil;
import com.pangda.common.utils.UserContext;
import com.pangda.pojo.DictData;
import com.pangda.pojo.DictType;
import com.pangda.pojo.data.DictTypeData;
import com.pangda.pojo.dto.DictTypeDto;
import com.pangda.pojo.dto.DictPageParam;
import com.pangda.pojo.vo.DictTypeVo;
import com.pangda.service.IDictDataService;
import com.pangda.service.IDictTypeService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 字典类型表 前端控制器
 * </p>
 * @author Panda.No72
 * @since 2024-06-20
 */
@RestController
@RequestMapping("/system/dict/type")
@Api(tags = "字典类型")
public class DictTypeController {
    @Autowired
    private IDictTypeService dictTypeService;

    @Autowired
    private IDictDataService dictDataService;
    @Autowired
    private RedisTemplate redisTemplate ;


    /**
     * 添加字典类型
     *
     * @param dictTypeDto
     * @return
     */
    @PostMapping
    public ResponseResult add(@RequestBody DictTypeDto dictTypeDto) {
        return dictTypeService.add(dictTypeDto);
    }

    /**
     * 分页条件查询
     * @param param
     * @return
     */
    @GetMapping("/list")
    public PageResult<DictTypeVo> list(DictPageParam param) {
        return dictTypeService.getList(param);
    }

    /**
     * 修改字典类型
     *
     * @param dictTypeDto
     * @return
     */
    @PutMapping()
    public ResponseResult update(@RequestBody DictTypeDto dictTypeDto) {
        dictTypeDto.setUpdateBy(UserContext.getUser());
        dictTypeDto.setUpdateTime(LocalDateTime.now());
        return ResponseResult.success(dictTypeService.updateById(BeanUtil.toBean(dictTypeDto, DictType.class)));
    }

    /**
     * 根据Id删除字典类型
     * @param dictIds
     * @return
     */
    @DeleteMapping("/{dictIds}")
    public ResponseResult remove(@PathVariable List<Integer> dictIds) {
        return ResponseResult.success(dictTypeService.removeByIds(dictIds));
    }

    /**
     * 根据Id查询字典类型
     * @param dictId
     * @return
     */
    @GetMapping("/{dictId}")
    public ResponseResult getInfo(@PathVariable Integer dictId) {
        return dictTypeService.getInfo(dictId);
    }

    /**
     * 状态下拉选项
     * @return
     */
    @GetMapping("/optionselect")
    public ResponseResult optionSelect() {
        List<DictData> list = dictDataService.list(new LambdaQueryWrapper<DictData>().select(DictData::getDictLabel));
        return ResponseResult.success(list);
    }

    /**
     * 字典类型导出
     * @return
     */
    @GetMapping("/export")
    public Map<String, String> typeExpert(DictPageParam pageParam) {
        List<DictType> list = dictTypeService.list();
        List<DictTypeData> dictTypeData = BeanUtil.copyToList(list, DictTypeData.class);
        Map<String, String> stringStringMap = FileUtil.uploadFile(dictTypeData, DictTypeData.class, redisTemplate);
        return stringStringMap;
    }



}
